<?php 
namespace App\Models;

use App\Cache\BaseCache;
use Log;

class Organization extends BaseModel{
	
	//表明
	protected $table = 'organization';
	//主键
	protected $primaryKey = 'id';
	
    //机构实体信息
    public function getOrganizationObejct($id,$organizationModel = null){
        $organization =$this->getOrganizationModel($id,$organizationModel);
        if(isset($organization->id) && $organization->is_delete != 1){
            $organizationObject = array(); 
            $organizationObject['organizationId'] = $organization->id;
            $organizationObject['nickname'] = $organization->nickname;
            $organizationObject['logo'] = $organization->logo;
            $organizationObject['bigLogo'] = $organization->big_logo;
            $organizationObject['introduction'] = $organization->introduction;
            $organizationObject['keywords'] = $organization->keywords;
            $organizationObject['imgQRCode'] = $organization->QR_code;
            $organizationObject['liveTotal'] = $organization->live_total;//直播总数
            $organizationObject['fansTotal'] = $organization->fans_total;//粉丝总数
            $organizationObject['attentionStatus'] = 0;//是否关注
            if($this->request->input('loginStatus') == 1){
                $cache = new BaseCache();
                $cache->method = 'get';
                $cache->cacheName = 'attention';
                $cache->cachePar = array(2,$organization->id,$this->request->input('userId')); //集合
                $retcache = $cache->HandleCache();
                if($retcache == 'NO_CACHE'){
                   $status = with(new Attention($this->request))->isAttention(2,$organization->id,$this->request->input('userId'));
                   if($status){
                       $organizationObject['attentionStatus'] = 1;
                   }
                }else if($retcache == 'ATTENTION'){
                    $organizationObject['attentionStatus'] = 1;
                }
            }    
            return $organizationObject; 
        }
        return;
    }
    
    //机构model
    public function getOrganizationModel($id,$organizationModel = null){
        $modelCache = new BaseCache();
        $modelCache->method = 'get';
        $modelCache->cacheName = 'organizationModel';
        $modelCache->cachePar = array($id); //集合
        $retCache = $modelCache->HandleCache();
        if ($retCache !== 'NO_CACHE') {
            if(!isset($retCache->id)){
                return;
            }
            return $retCache;
        }
        if(isset($organizationModel->id)){
            $organization = $organizationModel;
        }else if($id > 0){
            $organization = $this->select('organization.id','organization.nickname','organization.logo','organization.big_logo','organization.introduction','organization.keywords','organization.QR_code','organization.live_total','organization.fans_total','organization.status','organization.is_delete')
                ->where('id','=',$id)
                ->first();
        } else {
            $organization = 'CACHE_BREAKDOWN';
        }
        $modelCache->method = 'put';
        $modelCache->cacheValue = $organization;
        $modelCache->HandleCache();
        if(!isset($organization->id)){
            return;
        }
        return $organization;
    }
    
    
    public function getOrganizationInfo(){
        $id = $this->request->input('organizationId');
        if($id){
            return $this->getOrganizationObejct((int)$id);
        }
        return ;
    }
	
    //媒体专区
    public function getZhuanQu() {
        $category_id = (new RecommendCategory($this->request))->MEITI_ZHUANQUN;
        $organizations = Organization::select('organization.id','organization.nickname','organization.logo','organization.big_logo','organization.introduction','organization.keywords','organization.QR_code','organization.live_total','organization.fans_total','organization.status','organization.is_delete')
                    ->where('organization.is_delete','=','0')
                    ->join('recommend','recommend.link','=','organization.id')
                    ->where('recommend.category_id','=',$category_id)
                    ->where('recommend.is_delete','=','0')
                    ->orderBy('recommend.sort', 'asc')
                    ->take(10)
                    ->get();
        $data= array();
        foreach ($organizations as $key => $organization) {
            if(!isset($organization->id)){
                continue; 
            }
            $organizationModel = $this->getOrganizationObejct($organization->id,$organization);
            if ($organizationModel) {
                $data[] = $organizationModel;
            }
        }
        return $data;
    }
    
}